U.S. National Parks

I have had an interest in using R to create maps for a while now. I recently attended the September Maptime Meetup in Cambridge, MA where Ken Johnson walked the group through creating a slippy map using R and leaflet. The meeting gave me the inital skills I needed to create a basic map using the tools he introduced.

This map shows the locations of National Parks in the U.S. They are roughly split up by type of area, and the user can choose what types are shown on the map.

Below is the R code used to generate the map above. I used an R markdown file to create this webpage, which allows you to write and execute R code and then export your results and/or code to an html file.

#set R parameters
knitr::opts_chunk$set(echo=TRUE,fig.width=8, fig.height=4.5, comment=NA, warning=FALSE, message=FALSE)
options(width=100)

#import libraries
library(rgdal)
library(leaflet)

#read in data
data <-readOGR(dsn="nps_boundary.shp", layer="nps_boundary")

#project data
data_prj <-spTransform(data, CRS("+init=epsg:4326"))

#subset data into layers I want
nat_parks <- subset(data_prj, data_prj$UNIT_TYPE %in% c("National Park", "Park", "Park & Wilderness","National Recreation Area", "National Parkway", "National Recreation River", "National Scenic Trail", "Park", "Park & Wilderness", "Parkway"))
nat_pres <- subset(data_prj, data_prj$UNIT_TYPE %in% c("National Preserve", "Ecological and Historical Preserve", "National Historical Park & Ecological Preserve", " National Historical Park and Preserve", "National Reserve", "Preserve & Wilderness"))
nat_hist <- subset(data_prj, data_prj$UNIT_TYPE %in% c("Historical Park", "International Historic Site", "Memorial", "Memorial Parkway", "National Battlefield", "National Battlefield Park", "National Battlefield Site", "National Heritage Corridor", "National Historic Landmark District", "National Historic Park", "National Historic Site", "National Historical Park", "National Historical Site", "National Memorial", "National Military Park", "National Monument", "National Monument and Historic Shrine"))
nat_river <- subset(data_prj, data_prj$UNIT_TYPE %in% c("National River and Recreation Area", "National River", "National Scenic River", "National Scenic Riverway", "National Wild & Scenic River", "Navigable Rivers - State Owned ", "Scenic and Recreational River", "Wild and Scenic River"))
nat_shore <- subset(data_prj, data_prj$UNIT_TYPE %in% c("National Seashore", "Park - Water", "Park & Wilderness - Water", "National Lakeshore"))

#create popups for each layer
popups_parks = apply(nat_parks@data, 1, function(nat_parks) {
  paste0('<p>', '<strong>Name:<strong> ', nat_parks['UNIT_NAME'], '<br>', '<strong>Type:<strong> ', nat_parks['UNIT_TYPE'], '<br></p>')
  })
popups_pres = apply(nat_pres@data, 1, function(nat_pres) {
  paste0('<p>', '<strong>Name:<strong> ', nat_pres['UNIT_NAME'], '<br>', '<strong>Type:<strong> ', nat_pres['UNIT_TYPE'], '<br></p>')
  })
popups_hist = apply(nat_hist@data, 1, function(nat_hist) {
  paste0('<p>', '<strong>Name:<strong> ', nat_hist['UNIT_NAME'], '<br>', '<strong>Type:<strong> ', nat_hist['UNIT_TYPE'], '<br></p>')
  })
popups_river = apply(nat_river@data, 1, function(nat_river) {
  paste0('<p>', '<strong>Name:<strong> ', nat_river['UNIT_NAME'], '<br>', '<strong>Type:<strong> ', nat_river['UNIT_TYPE'], '<br></p>')
  })
popups_shore = apply(nat_shore@data, 1, function(nat_shore) {
  paste0('<p>', '<strong>Name:<strong> ', nat_shore['UNIT_NAME'], '<br>', '<strong>Type:<strong> ', nat_shore['UNIT_TYPE'], '<br></p>')
  })

#set the map
map <- leaflet() %>% 
  addProviderTiles('Acetate.terrain') %>%
  setView(-110.333324, 49.557814, zoom = 2) %>%
  addPolygons(data=nat_parks, 
              stroke = TRUE,
              color = '#544d56',
              fillOpacity = 0.7, 
              smoothFactor = 0.7,
              fillColor = '#006837',
              weight = .7,
              popup=unname(popups_parks), 
              options=c(pathOptions(),popupOptions(minWidth=50)),
              group = "National Parks") %>%
  addPolygons(data=nat_pres, 
              stroke = TRUE,
              color = '#544d56',
              fillOpacity = 0.7, 
              smoothFactor = 0.7,
              fillColor = '#004529',
              weight = .7,
              popup=unname(popups_pres), 
              options=c(pathOptions(),popupOptions(minWidth=50)),
              group = "National Preserves") %>%
  addPolygons(data=nat_river, 
              stroke = TRUE,
              color = '#544d56',
              fillOpacity = 0.7, 
              smoothFactor = 0.7,
              fillColor = '#41ab5d',
              weight = .7,
              popup=unname(popups_river), 
              options=c(pathOptions(),popupOptions(minWidth=50)),
              group = "National Riverways") %>%
  addPolygons(data=nat_shore, 
              stroke = TRUE,
              color = '#544d56',
              fillOpacity = 0.7, 
              smoothFactor = 0.7,
              fillColor = '#78c679',
              weight = .7,
              popup=unname(popups_shore), 
              options=c(pathOptions(),popupOptions(minWidth=50)),
              group = "National Shores") %>%
  addPolygons(data=nat_hist, 
              stroke = TRUE,
              color = '#544d56',
              fillOpacity = 0.7, 
              smoothFactor = 0.7,
              fillColor = '#238443',
              weight = .7,
              popup=unname(popups_hist), 
              options=c(pathOptions(),popupOptions(minWidth=50)),
              group = "National Historical Sites") %>%
#add layers control to allow user to decide what layers show
  addLayersControl(
              overlayGroups = c("National Parks", "National Preserves", "National Riverways", "National Shores", "National Historical Sites"),
              options = layersControlOptions(collapsed = FALSE)
            )
map